# This module can be mixedin to a model class to give it common operations associated with quantities
module PBBG
  module Quantifiable
    # add a quantity from this object
    def add!(quantity)
      update_attributes!(:quantity => self.quantity + quantity)
      self
    end
    # remove a quantity from this object
    def remove!(quantity)
      current = self.quantity
      if current > quantity
        add!(0 - quantity)
      elsif current < quantity
        raise "Quantity greater than available"
      else
        destroy
      end
    end
    # set the quantity directly
    def set!(quantity)
      if quantity > 0
        update_attributes!(:quantity => quantity)
      else
        destroy
      end
    end
  end
end
